<?php

if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );
class get_return_info_robot extends CI_Controller {
    public function __construct() {
        ignore_user_abort ( true );
        set_time_limit ( 0 );
        parent::__construct ();
    }
    
    /**
     *
     *
     * ription 从商家获取返回数据
     * 
     * @name @date 2012-10-22
     * @author eifel
     * @param            
     *
     * @return
     *
     * @version
     *
     */
    public function index() {
        $this->level_one ();
        $this->level_two ();
        $this->level_three ();
        $this->level_four ();
        $this->level_five ();
    }
    public function level_one() {
        $this->load->model ( "mpay_transaction_model" );
        $to_get_return_list = $this->mpay_transaction_model->time_space_unreturn_order ( "00:00:10", "00:00:30" );
        if ($to_get_return_list != null) {
            foreach ( $to_get_return_list as $list ) {
                $this->notify ( $list );
            }
        }
        // sleep(30);
        // $this->level_one();
    }
    public function level_two() {
        $this->load->model ( "mpay_transaction_model" );
        $to_get_return_list = $this->mpay_transaction_model->time_space_unreturn_order ( "00:00:30", "00:01:00" );
        if ($to_get_return_list != null) {
            foreach ( $to_get_return_list as $list ) {
                $this->notify ( $list );
            }
        }
        // sleep(60);
        // $this->level_two();
    }
    public function level_three() {
        $this->load->model ( "mpay_transaction_model" );
        $to_get_return_list = $this->mpay_transaction_model->time_space_unreturn_order ( "00:01:00", "00:10:00" );
        if ($to_get_return_list != null) {
            foreach ( $to_get_return_list as $list ) {
                $this->notify ( $list );
            }
        }
        // sleep(600);
        // $this->level_three();
    }
    public function level_four() {
        $this->load->model ( "mpay_transaction_model" );
        $to_get_return_list = $this->mpay_transaction_model->time_space_unreturn_order ( "00:10:00", "01:00:00" );
        if ($to_get_return_list != null) {
            foreach ( $to_get_return_list as $list ) {
                $this->notify ( $list );
            }
        }
        // sleep(3600);
        // $this->level_four();
    }
    public function level_five() {
        $this->load->model ( "mpay_transaction_model" );
        $to_get_return_list = $this->mpay_transaction_model->time_space_unreturn_order ( "01:00:00", "24:00:00" );
        if ($to_get_return_list != null) {
            foreach ( $to_get_return_list as $list ) {
                $this->notify ( $list );
            }
        }
        // sleep(3600*24);
        // $this->level_five();
    }
    private function notify($list) {
        $url_string = "MerId=" . $list ['MERCHANT_ID'];
        $url_string = $url_string . "&OrderId=" . $list ['ORDER_ID'];
        $url_string = $url_string . "&Money=" . $list ['MONEY'];
        $url_string = $url_string . "&TranCode=" . $list ['TRAN_CODE'];
        $url_string = $url_string . "&EncString=" . md5 ( $list ['MERCHANT_ID'] . $list ['ORDER_ID'] . $list ['MONEY'] . $list ['MERCHANT_KEY'] );
        $url_string = $url_string . "&PaymentFee=" . $list ['PAYMENT_FEE'];
        $url_string = $url_string . "&Note=" . $list ['NOTE'];
        $url_string = $url_string . "&PaymentStatusCode=" . $list ['PAYMENT_PAY_STATUS'];
        if ($list ['RETURN_URL']) {
            $url = $list ['RETURN_URL'] . "?" . $url_string;
        } else {
            $url = $list ['SERVER_NOTIFY_URL'] . "?" . $url_string;
        }
        
        log_message ( "info", $url );
        /**
         * 用CURL来读取数据
         */
        $ch = curl_init ();
        $timeout = 2;
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        $contents = curl_exec ( $ch );
        curl_close ( $ch );
        
        $contents = strtoupper ( substr ( trim ( $contents ), 0, 7 ) );
        
        if ($contents == "SUCCESS") {
            
            $this->load->model ( "mpay_transaction_model" );
            $this->mpay_transaction_model->change_transaction_retrun ( $list ['TRAN_ID'], 1 );
        }
    }
    public function test() {
        $this->load->model ( "mpay_transaction_model" );
        $to_get_return_list = $this->mpay_transaction_model->time_space_unreturn_order ( "00:00:00", "24:00:00" );
        if ($to_get_return_list != null) {
            foreach ( $to_get_return_list as $list ) {
                $this->notify ( $list );
            }
        }
    }
}